<?php

/***********************
Авторизация Пользователя
@return json, boolean
***********************/
class auth_business
{	
	public function __construct() {
		$this->parent_class = new auth;		
	}
	
	public function logout_user() {
		$this->parent_class->Logout();
		header('location: /');
		exit();	
	}
	
	public function auth()
	{		
		if(!$this->parent_class->Check_Auth())
		{
			/* Если FALSE то авторизация не происходит */
			$AuthCheck = FALSE;
		
			$username = GetParam($_POST, 'username', '');
			$password = GetParam($_POST, 'password', '');
			$sumbit   = GetParam($_POST, 'a', '');
			
			/* Из формы отправка через Ajax */
			if(!empty($sumbit) && ($sumbit == 'send'))
			{
				$this->parent_class->Logout();
				if($this->parent_class->Login($username, $password))
				{
					/* Отправка JSON success */
					if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' : false) {			
						$ajaj_reply = array(
							"valid"    => true,
							"redirect" => "/".START_PAGE.ENDURL
						);
						header("Content-Type: text/json; charset=utf-8");
						exit(getJSON($ajaj_reply));
					}					
					$AuthCheck = TRUE;				
				}
				else
				{
					/* Отправка JSON failure */
					$ajaj_reply = array(
						"valid" => false,
						"error" => "Не правильный логин / пароль!"
					);
					header("Content-Type: text/json; charset=utf-8");
					exit(getJSON($ajaj_reply));
				}
			}		
			if(!$AuthCheck) {				
				exit(Registry::get("smarty")->display(TEMPLATE_SITE.'login.tpl'));			
			}			
		}
		else {
			return true;
		}
	}	
}
     
?>
